package com.charwayh.hellomybatisplus.config;

import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * @author charwayH
 */

@MapperScan("com.charwayh.hellomybatisplus.mapper") //扫描包
@EnableTransactionManagement//自动管理事务注解
@Configuration//配置类
public class MyBatisPlusConfig {
    /**
     * 乐观锁拦截器
     * @return
     */
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor(){
        return new OptimisticLockerInterceptor();
    }

    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

    @Bean
    public ISqlInjector sqlInjector(){
        return new LogicSqlInjector();
    }

    /**
     * 性能分析插件——————sql执行效率插件
     */
    @Bean
    @Profile({"dev","test"})
    public PerformanceInterceptor performanceInterceptor() {
        PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
        // 在工作中，不允许用户等待  （分别测试 1ms:1毫秒    1000ms:1秒）
        // ms 设置sql执行的最大s时间,如果超过了则不执行
        performanceInterceptor.setMaxTime(10000);
        // 是否进行格式化
        performanceInterceptor.setFormat(true);
        return new PerformanceInterceptor();
    }
}